package yja.gzw.app.activity

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.R
import yja.gzw.app.adapter.FabuAdapter
import yja.gzw.app.adapter.ZhuanquAdapter
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivitySiyuanBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.OrderData
import yja.gzw.app.viewmodel.Mainviewmodel

class SiyuanActivity :BaseActivity<Mainviewmodel, ActivitySiyuanBinding>(R.layout.activity_siyuan){
    private lateinit var fabiada:ZhuanquAdapter
    var yeshu=1
    var shuju= mutableListOf<OrderData>()
    override fun initView() {
            binding.fanhui.setOnClickListener {
                finish()
            }
        binding.sousuoanniu.setOnClickListener {
            //Toast.makeText(this, "点击了搜索", Toast.LENGTH_SHORT).show()
        }

        binding.fabuanniu.setOnClickListener {

            var intent=Intent(this,FabusiActivity::class.java)
            startActivity(intent)
        }
        val neirong=binding.sousuo.text.toString()
        binding.sousuo.setOnClickListener {

            val intent=Intent(this,SousuoActivity::class.java)
            intent.putExtra("type","2")
            startActivity(intent)

       }


        viewmodel.onzhuanquliebiaowang(UserData.token,mapOf("keyword" to neirong,"type" to "2","page" to "1","pagesize" to "10"))
        binding.refreshLayout.setRefreshHeader(ClassicsHeader(this));
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(this));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                shuju.clear()
                yeshu=1
                viewmodel.onzhuanquliebiaowang(UserData.token,mapOf("keyword" to neirong,"type" to "2","page" to "1","pagesize" to "10"))

            }


        })

        binding.refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {

                if(yeshu==((shuju.size/10)+1)){

                }else{
                    yeshu++
                }

                viewmodel.onzhuanquliebiaowang(UserData.token,mapOf("keyword" to neirong,"type" to "2","page" to yeshu.toString(),"pagesize" to "10"))

            }


        })


    }

    override fun onResume() {
        super.onResume()


        viewmodel.onzhuanquliebiaowang(UserData.token,mapOf("keyword" to "","type" to "2","page" to "1","pagesize" to "10"))


    }

    override fun dataObservese() {
        viewmodel.onzhuanquliebiao.observe(this, Observer {
            if(it.code==1){

                if(it.data.data.size<10&&yeshu==1){

                    binding.review.layoutManager=
                        LinearLayoutManager(this)
                    fabiada= ZhuanquAdapter(this,it.data.data)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()

                }else{
                    shuju.addAll(it.data.data)
                    binding.review.layoutManager=
                        LinearLayoutManager(this)
                    fabiada= ZhuanquAdapter(this,shuju)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()
                    if(yeshu==1){
                        binding.review.scrollToPosition(0)

                    }else{
                        val shshbu=10*(yeshu-1)
                        binding.review.scrollToPosition(shshbu-2)

                    }
                }




            }else{

                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }


        })


//        viewmodel.onzhuanquliebiao.observe(this, Observer {
//            if(it.code==1){
//                Log.e("数据展示",it.data.data.toString())
//                binding.review.layoutManager=
//                    LinearLayoutManager(this)
//                fabiada= ZhuanquAdapter(this,it.data.data)
//                binding.review.adapter= fabiada
//
//
//
//            }else{
//
//                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
//
//            }
//
//
//        })

    }
}